package cn.ffcs.bpmpvc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.echoice.hibernate.HibernateEntityDao;
import org.echoice.paper.PageBean;
import org.springframework.jdbc.core.RowMapper;

import cn.ffcs.bpmpvc.dao.MeetingProtocolDao;
import cn.ffcs.share.model.ShareReference;

public class MeetingProtocolDaoImpl extends HibernateEntityDao<ShareReference> implements MeetingProtocolDao{
	public ShareReference getByPID(String procinstId){
		String hql="select t from ShareReference t where t.procinstId=?";
		List list=getHibernateTemplate().find(hql, procinstId);
		if(list!=null&&list.size()>0){
			return (ShareReference)list.get(0);
		}
		return null;
	} 
	
	public PageBean searchMeetingProtocolPage(ShareReference searchForm,int pageNo,int pageSize){
		StringBuilder hql=new StringBuilder();
		hql.append("select t from ShareReference t where t.procinstId is not null");
		List paramList=new ArrayList();
		if(StringUtils.isNotBlank(searchForm.getTitle())){
			hql.append(" and t.title like ?");
			paramList.add("%"+searchForm.getTitle()+"%");
		}
		
		if(StringUtils.isNotBlank(searchForm.getReqCode())){
			hql.append(" and t.reqCode = ?");
			paramList.add(searchForm.getReqCode());
		}
		hql.append(" order by t.begin desc");
		return super.pagedQueryHQL(hql.toString(), pageNo, pageSize, paramList.toArray());
	}
	
	public String[] findPersonList(String parentOrgId,String excludePersonId){
		String sql="select distinct t2.id from t_organ_org_rela_person t1, t_organ_person t2,"
				+"(select * from t_organ_organization t start with t.id = ? connect by prior id = t.superior) t3"
				+" where t1.person = t2.id and t1.organization = t3.id and t2.id not in("+excludePersonId+")";
		List list=getJdbcTemplate().query(sql, new Object[]{parentOrgId}, new RowMapper(){

			public Object mapRow(ResultSet rs, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				return rs.getString("id");
			}});
		String personIdArr[]=null;
		if(list!=null&&list.size()>0){
			personIdArr=new String[list.size()];
			int i=0;
			for (Object tmp : list) {
				personIdArr[i]=(String)tmp;
				i++;
			}
		}
		return personIdArr;
	}
	
	public void deleteById(String id){
		String sql="delete from t_busi_share_reference t where t.id=?";
		getJdbcTemplate().update(sql, new Object[]{id});
	}
}
